package com.rainripple.java.common.dao;

import java.util.List;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

@Component
public class MybatisBaseDaoImpl<T, E> implements BaseDao<T, E> {

	private SqlSessionTemplate template;

	public SqlSessionTemplate getTemplate() {
		return template;
	}

	@Resource(name = "sqlSession")
	public void setTemplate(SqlSessionTemplate template) {
		this.template = template;
	}

	@Override
	public void save(T entity) {
		if (null != entity)
			template.insert(getMapper(entity.getClass()) + MAPPER_INSERT,
					entity);
	}

	@Override
	public T get(Class<T> entityClass, E id) {
		return template.selectOne(getMapper(entityClass) + MAPPER_GET, id);
	}

	@Override
	public List<T> getAll(T prarm) {
		return template.selectList(getMapper(prarm.getClass()) + MAPPER_GETALL,
				prarm);
	}

	private String getMapper(Class<?> entityClass) {
		return entityClass.getSimpleName() + "Mapper.";
	}

	private static String MAPPER_INSERT = "save";
	private static String MAPPER_GET = "get";
	private static String MAPPER_GETALL = "search";

}
